import {
  DirectoryTreeNode,
  FileIndexItem,
  OpenedFileRecord,
  OperationResult,
  TreeWalkOptions
} from "../../filespace/src";

export type NoteType = 'markdown' | 'richtext'

// 这些类型定义会在 preload.ts 中通过 contextBridge 暴露给渲染进程
declare global {
  interface Window {
    bnote: {
      ready: {
        settings: () => Promise<void>
        fileSpace: () => Promise<void>
      }
      menus: {
        command: (key: string, x: number, y: number) => Promise<void>,
        onSetting: (cb: Function) => void
      }
      settings: {
        load: () => Promise<UserSettings>
        save: (settings: Partial<UserSettings>) => Promise<void>
      }
      files: {
        getHistory: (fileId: string) => Promise<NoteHistory[]>
        selectPath: () => Promise<string | null>
        create: (name: string, content?: string, parentId?: string) => Promise<OperationResult<FileIndexItem>>
        load: (fileId: string) => Promise<string>
        save: (fileId: string, content: string) => Promise<boolean>
        delete: (fileId: string) => Promise<boolean>
        move: (fileId: string, parentId?: string) => Promise<boolean>,
        addOrRemoveOpen: (fileId: string) => Promise<boolean>,
        openeds: () => Promise<OpenedFileRecord[]>
      }
      dirs: {
        create: (name: string, parentId?: string) => Promise<OperationResult<FileIndexItem>>
        delete: (dirId: string) => Promise<boolean>
        move: (dirId: string, parentId?: string) => Promise<boolean>
        childs: (dirId: string) => Promise<FileIndexItem[]>
        root: () => Promise<DirectoryTreeNode|null>
        build: (startPath?: string, options?: TreeWalkOptions) => Promise<DirectoryTreeNode|null>
        tree: () => Promise<DirectoryTreeNode>
        treeLazy: (dirId: string) => Promise<DirectoryTreeNode>
        openedIds: () => Promise<string[]>
      }
      temps: {
        create: (name: string, parentId?: string, content?: string) => Promise<FileIndexItem | null>
        load: (fileId: string) => Promise<string>
        save: (fileId: string, content: string) => Promise<boolean>
        delete: (fileId: string) => Promise<boolean>
        all: () => Promise<FileIndexItem[]>
      },
      contextmenus: {
        common: () => Promise<void>
        noteDirs: (item: DirectoryTreeNode) => Promise<void>
        noteFiles: (item: DirectoryTreeNode) => Promise<void>
        onNoteDirs: (cb: (command: ContextmenusNoteDirsCommand, item: DirectoryTreeNode) => void) => void
        onNoteFiles: (cb: (command: ContextmenusNoteFilesCommand, item: DirectoryTreeNode) => void) => void
      }
    }
  }
}

export interface Note {
  id: string
  title: string
  content: string
  type: NoteType
  created: number
  updated: number
  tags: string[]
  folder: string
  encrypted: boolean
}

// 笔记索引类型
export interface NoteIndex {
  id: string
  title: string
  path: string
  type: NoteType
  folder: string
  created: number
  updated: number
  tags: string[]
  encrypted: boolean
}

export interface NoteFolder {
  id: string
  name: string
  parent: string | null
  created: number
  updated: number
}

export interface UserSettings {
  general: {
    autoUpdate: boolean
    debugMode: boolean
    language: string
  }
  editor: {
    imageStoragePath: string
    defaultNoteType: string
    notebookPath: string
    enableMultipleNotes: boolean  // 是否允许笔记多开
    maxOpenNotes: number         // 多开最大数量
    wrapTabs: boolean           // 标签是否换行
    autoSaveInterval: number    // 自动保存间隔（秒）
    maxHistoryCount: number     // 最大历史记录数
    markdownConfig: {
      livePreview: boolean
      codeHighlight: boolean
    }
  }
  sync: {
    enabled: boolean
    type: string
    serverUrl: string
  }
  privacy: {
    fileEncryption: boolean
    transmissionEncryption: boolean
    timeoutProtection: boolean
    timeoutDuration: number
  }
  appearance: {
    theme: 'auto' | 'dark' | 'light'
    fontSize: number
    fontFamily: string
    colors: {
      primary: string
      secondary: string
      success: string
      warning: string
      danger: string
      info: string
    }
    background: {
      type: 'color' | 'image'
      value: string
      opacity: number
    }
  }
}

// 笔记历史记录
export interface NoteHistory {
  id: string           // 历史记录ID
  noteId: string       // 笔记ID
  title: string        // 标题
  content: string      // 内容
  timestamp: number    // 时间戳
  type: NoteType       // 笔记类型
}

// 打开的笔记状态
export interface OpenedNote {
  id: string           // 笔记ID
  title: string        // 标题
  content: string      // 内容
  type: NoteType       // 类型
  saved: boolean       // 是否已保存
  modified: boolean    // 是否已修改
  folder: string       // 所属文件夹
  path?: string        // 文件路径（如果已保存）
  history: NoteHistory[] // 历史记录
  lastSaved?: number   // 最后保存时间
  lastModified: number // 最后修改时间
}

// 文件夹信息
export interface FolderInfo {
  id: string
  name: string
  path: string
  parentId: string | null
}

// 树节点
export interface TreeNode {
  id: string
  name: string
  isFolder: boolean
  children?: TreeNode[]
  type?: string
  path?: string
}
